home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacWorld 1999 June
/
Macworld (1999-06).dmg
/
Shareware World
/
Info
/
For Developers
/
MacZoop2.0.sea
/
MacZoop2.0
/
Required Classes
/
ZTimer.cpp
< prev
next >
Wrap
Text File
|
1999-02-11
|
4KB
|
218 lines
/*************************************************************************************************
*
*
* MacZoop - "the framework for the rest of us"
*
*
*
* ZTimer.cpp -- simple timer object
*
*
*
*
*
* © 1998, Graham Cox
*
*
*
*
*************************************************************************************************/
#include "ZTimer.h"
#include "ZWindow.h"
#include "MacZoop.h"
static long gTimerIDSeed = 20000;
static ZTimerQueue* gTimerQ = NULL;
static unsigned long gTickCount = 0;
void TimerTimer();
/*--------------------------------*** CONSTRUCTOR ***---------------------------------*/
ZTimer::ZTimer( unsigned long tRate, long anID, ZCommander* owner, Boolean isOneShot )
: ZComrade()
{
wOwner = owner;
id = anID;
oneShot = isOneShot;
if ( id == 0 )
id = ++gTimerIDSeed;
interval = tRate;
lastTicks = gTickCount;
}
/*------------------------------------*** DO ***--------------------------------------*/
/*
check if time to fire and call window or broadcast message
----------------------------------------------------------------------------------------*/
void ZTimer::Do()
{
// check if time has elapsed or if tickcount has wrapped...
if ( gTickCount >= ( lastTicks + interval ) || ( gTickCount < lastTicks ))
{
lastTicks = gTickCount;
if ( wOwner )
{
// if the owner's a window, focus it.
if ( dynamic_cast<ZWindow*>( wOwner ))
((ZWindow*) wOwner )->Focus();
wOwner->DoTimer( id );
}
else
SendMessage( kTimerMsgTimerTripped, (void*) id );
// if one shot, destroy after firing
if ( oneShot )
{
// delete from list
gTimerQ->DeleteObject( this );
if ( gTimerQ->CountItems() <= 0 )
ForgetObject( gTimerQ );
ForgetThis();
}
}
}
/*----------------------------------*** SETTIMER ***----------------------------------*/
/*
creates timer object and installs it into the timer queue
----------------------------------------------------------------------------------------*/
ZTimer* SetTimer( ZCommander* aCmdr, long id, unsigned long interval, Boolean isOneShot )
{
ZTimer* zt;
if ( gTimerQ == NULL )
FailNIL( gTimerQ = new ZTimerQueue());
FailNIL( zt = new ZTimer( interval, id, aCmdr, isOneShot ));
gTimerQ->AppendItem( zt );
return zt;
}
/*---------------------------------*** KILLTIMER ***----------------------------------*/
/*
removes timer from the queue and discards it
----------------------------------------------------------------------------------------*/
void KillTimer( ZCommander* aCmdr, long id )
{
long iMax;
ZTimer* zt;
if ( gTimerQ )
{
iMax = gTimerQ->CountItems();
while ( iMax )
{
zt = (ZTimer*) gTimerQ->GetObject( iMax-- );
if ( zt && ( zt->GetID() == id ) && ( zt->GetOwner() == aCmdr ))
{
gTimerQ->DeleteObject( zt );
ForgetObject( zt );
break;
}
}
if ( gTimerQ->CountItems() <= 0 )
ForgetObject( gTimerQ );
}
}
/*-------------------------------*** KILLALLTIMERS ***--------------------------------*/
/*
removes and deletes all timers associated with a particular window. Called when a window
is destructed.
----------------------------------------------------------------------------------------*/
void KillAllTimers( ZCommander* aCmdr )
{
long iMax;
ZTimer* zt;
if ( gTimerQ )
{
iMax = gTimerQ->CountItems();
while ( iMax )
{
zt = (ZTimer*) gTimerQ->GetObject( iMax-- );
if ( zt && zt->GetOwner() == aCmdr )
{
gTimerQ->DeleteObject( zt );
ForgetObject( zt );
}
}
if ( gTimerQ->CountItems() <= 0 )
ForgetObject( gTimerQ );
}
}
/*---------------------------------*** TIMERTIMER ***---------------------------------*/
/*
called repeatedly by ZApplication to iterate the timer queue and trigger timers. Note that
this iterates in reverse so that if a timer responder deletes the timer, it will continue
to trigger the remaining timers.
----------------------------------------------------------------------------------------*/
void TimerTimer()
{
// copy the tickcount to a global so the iteration can proceed as fast as possible
gTickCount = TickCount();
long iMax;
ZTimer* zt;
if ( gTimerQ )
{
iMax = gTimerQ->CountItems();
while( iMax )
{
zt = (ZTimer*) gTimerQ->GetObject( iMax-- );
if ( zt )
zt->Do();
}
}
}